package yl.hs.bmipfull.service.flow.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.flow.IFlowDataDao;
import yl.hs.bmipfull.daos.flow.IFlowUserTaskDao;
import yl.hs.bmipfull.daos.material.IWarehouseDao;
import yl.hs.bmipfull.daos.production.IFinishProductInventoryDao;
import yl.hs.bmipfull.daos.production.IFinishProductWarehouseRecordDao;
import yl.hs.bmipfull.daos.production.IProductionTaskDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractDao;
import yl.hs.bmipfull.daos.system.IGlobalSearchDao;
import yl.hs.bmipfull.daos.system.IPojoSvcDao;
import yl.hs.bmipfull.pojo.flow.*;
import yl.hs.bmipfull.pojo.material.WarehouseEntity;
import yl.hs.bmipfull.pojo.production.FinishProductInventoryEntity;
import yl.hs.bmipfull.service.flow.*;
import yl.hs.bmipfull.service.system.impl.SystemBaseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.*;
import yl.hs.bmipfull.viewmodels.LoginUser;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.flow.FlowDataHandleViewModel;

import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作用:  流程数据表服务层接口实现<br/>
 * 创建时间: 2021年06月23日 18时19分40秒 <br/>
 *
 * @author 张凯
 */
@Service
public class FlowDataServiceImpl extends SystemBaseService implements IFlowDataService {

    final
    IFlowDataDao dao;

    final
    IFlowInfoService flowInfoService;

    final
    IFlowConditionService conditionService;

    final
    IFlowNodeService nodeService;

    final
    IFlowLogService flowLogService;

    final
    IFlowNodeHandlersService handlersService;

    final
    IFlowUserTaskDao userTaskDao;

    final
    IGlobalSearchDao globalSearchDao;
    final
    IPojoSvcDao pojoSvcDao;

    public FlowDataServiceImpl(IFlowDataDao dao, IFlowInfoService flowInfoService, IFlowConditionService conditionService, IFlowNodeService nodeService, IFlowLogService flowLogService, IFlowNodeHandlersService handlersService, IFlowUserTaskDao userTaskDao, IGlobalSearchDao globalSearchDao, IPojoSvcDao pojoSvcDao) {
        this.dao = dao;
        this.flowInfoService = flowInfoService;
        this.conditionService = conditionService;
        this.nodeService = nodeService;
        this.flowLogService = flowLogService;
        this.handlersService = handlersService;
        this.userTaskDao = userTaskDao;
        this.globalSearchDao = globalSearchDao;
        this.pojoSvcDao = pojoSvcDao;
    }


    @Override
    public List<FlowDataEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(FlowDataEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<FlowDataEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(FlowDataEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<FlowDataEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public FlowDataEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveFlowData(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        FlowDataEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new FlowDataEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(FlowDataEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入流程数据表信息", Tl.toJson(entity), FlowDataEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);

            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新流程数据表信息", Tl.toJson(entity), FlowDataEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, FlowDataEntity.class);
        }
        return result;
    }

    private final String stateField = "state";
    private final String snField = "sn";

    @Override
    public Integer handle(FlowDataHandleViewModel dataHandle) throws Exception {
        var userTask = userTaskDao.findBySN(dataHandle.getFlowTaskSn());
        if (userTask.getTaskState() != 0) {
            return 0;
        }
        LoginUser user = HsApp.getCurrent();
        FlowDataEntity flowData = dao.findBySN(dataHandle.getFlowDataSn());
        FlowNodeEntity nowNode = nodeService.findBySn(flowData.getNodeSn());
        List<FlowNodeHandlersEntity> handlers = handlersService.findByNodeSn(flowData.getNodeSn());
        if (user != null) {
            flowLogService.saveFlowLog(new FlowLogEntity(flowData.getFlowSn(), user.getUserSN(), flowData.getSn(),
                    user.getUserName(), new Date(), dataHandle.getPassResult(), dataHandle.getRemark(), flowData.getNodeSn(), FlowActionType.APPROVAL.code));
        }
        List<FlowLogEntity> flowLogList = flowLogService.queryByFlowDataSn(flowData.getSn());
        if (dataHandle.getPassResult() == FlowDataHandleType.Ok.code) {
            //同意
            FlowConditionEntity condition = conditionService.findByLeftNodeSn(flowData.getNodeSn());
            if (nowNode.getPassType() == FlowNodePassType.Or.getCode()) {
                flowPassed(flowData, condition);
                if (flowData.getState() != FlowDataState.Over.code) {
                    //流程继续
                    List<FlowNodeHandlersEntity> nodeHandlersEntities = handlersService.findByNodeSn(flowData.getNodeSn());
                    List<FlowUserTaskEntity> flowUserTaskList = userTaskDao.queryListByObjectSN(flowData.getDataSn());
                    //创建消息提醒
                    sendMsg(MsgType.Flow, flowUserTaskList.get(0).getUserSN(), "审批提醒", user.getUserName() + "同意了" + flowUserTaskList.get(0).getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark());
                    //结束此数据的所有代办，进入下个节点
                    userTaskDao.updateStateByObjectSN(flowData.getDataSn());
                    for (var handlerEntity : nodeHandlersEntities) {
                        FlowUserTaskEntity userTaskEntity = new FlowUserTaskEntity();
                        userTaskEntity.setHandlerUserSN(handlerEntity.getParticipantSN());
                        userTaskEntity.setUserSN(flowUserTaskList.get(0).getUserSN());
                        userTaskEntity.setUserName(flowUserTaskList.get(0).getUserName());
                        userTaskEntity.setTaskState(FlowTaskState.UNREAD.code);
                        userTaskEntity.setDataType(flowUserTaskList.get(0).getDataType());
                        userTaskEntity.setDataCode(flowUserTaskList.get(0).getDataCode());
                        userTaskEntity.setFlowSN(flowData.getFlowSn());
                        userTaskEntity.setObjectSN(flowData.getDataSn());
                        userTaskEntity.setFlowNodeSN(flowData.getNodeSn());
                        userTaskEntity.setCreateTime(new Date());
                        userTaskEntity.setSn(queryNewSN(FlowUserTaskEntity.class));
                        //创建消息提醒
                        sendMsg(MsgType.Flow, handlerEntity.getParticipantSN(), "审批提醒", flowUserTaskList.get(0).getUserName() + "向你发起了" + flowUserTaskList.get(0).getDataType() + "的审批,数据编号为" + flowData.getDataSn());
                        //待办事项创建
                        userTaskDao.insertNew(userTaskEntity);
                    }
                } else {
                    //流程结束
//                    List<FlowUserTaskEntity>  flowUserTaskList=userTaskDao.queryListByFlowNodeSN(flowData.getNodeSn());
//                    for (var userTask:flowUserTaskList) {
//                        userTask.setTaskState(FlowTaskState.APPROVED.code);
//                        userTaskDao.update(userTask);
//                    }
                    FlowUserTaskEntity flowUserTask = userTaskDao.findBySN(dataHandle.getFlowTaskSn());
                    //创建消息提醒
                    sendMsg(MsgType.Flow, flowUserTask.getUserSN(), "审批提醒", user.getUserName() + "同意了" + flowUserTask.getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark() + "。流程结束！");
                    flowUserTask.setTaskState(FlowTaskState.APPROVED.code);
                    flowUserTask.setRemark(dataHandle.getRemark());
                    flowUserTask.setResult(dataHandle.getPassResult());
                    flowUserTask.setProcessTime(new Date());
                    userTaskDao.update(flowUserTask);
                    pojoSvcDao.updateBySn(flowData.getDataType(), (short) 2, flowData.getDataSn(), stateField, snField);
                    if (flowData.getDataType().equals("finish_product_warehouse_record")) {
                        updataKc(flowData);
                    }
                    //结束此数据的所有代办，流程结束
                    userTaskDao.updateStateByObjectSN(flowData.getDataSn());

                }
            } else if (nowNode.getPassType() == FlowNodePassType.And.getCode()) {
                //等待其他人全部通过
                List<FlowLogEntity> okFlowLog = flowLogList.stream().filter(t -> t.getResult() == FlowDataHandleType.Ok.code && t.getActionType() == FlowActionType.APPROVAL.code).toList();
                boolean passResult = matchFlowHandlerUserSn(handlers, okFlowLog);

                if (passResult) {
                    flowPassed(flowData, condition);
                    FlowConditionEntity condition2 = conditionService.findByLeftNodeSn(flowData.getNodeSn());
                    flowPassed(flowData, condition2);
                    if (flowData.getState() != FlowDataState.Over.code) {
                        //流程继续
                        List<FlowNodeHandlersEntity> nodeHandlersEntities = handlersService.findByNodeSn(flowData.getNodeSn());
                        List<FlowUserTaskEntity> flowUserTaskList = userTaskDao.queryListByObjectSN(flowData.getDataSn());
                        //创建消息提醒
                        sendMsg(MsgType.Flow, flowUserTaskList.get(0).getUserSN(), "审批提醒", user.getUserName() + "同意了" + flowUserTaskList.get(0).getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark());
                        //结束此数据的所有代办，进入下个节点
                        userTaskDao.updateStateByObjectSN(flowData.getDataSn());
                        for (var handlerEntity : nodeHandlersEntities) {
                            FlowUserTaskEntity userTaskEntity = new FlowUserTaskEntity();
                            userTaskEntity.setHandlerUserSN(handlerEntity.getParticipantSN());
                            userTaskEntity.setUserSN(flowUserTaskList.get(0).getUserSN());
                            userTaskEntity.setUserName(flowUserTaskList.get(0).getUserName());
                            userTaskEntity.setTaskState(FlowTaskState.UNREAD.code);
                            userTaskEntity.setDataType(flowUserTaskList.get(0).getDataType());
                            userTaskEntity.setDataCode(flowUserTaskList.get(0).getDataCode());
                            userTaskEntity.setFlowSN(flowData.getFlowSn());
                            userTaskEntity.setObjectSN(flowData.getDataSn());
                            userTaskEntity.setFlowNodeSN(flowData.getNodeSn());
                            userTaskEntity.setCreateTime(new Date());
                            userTaskEntity.setSn(queryNewSN(FlowUserTaskEntity.class));
                            //待办事项创建
                            userTaskDao.insertNew(userTaskEntity);
                            //创建消息提醒
                            sendMsg(MsgType.Flow, handlerEntity.getParticipantSN(), "审批提醒", flowUserTaskList.get(0).getUserName() + "向你发起了" + flowUserTaskList.get(0).getDataType() + "的审批,数据编号为" + flowData.getDataSn());
                        }
                    } else {
                        //流程结束
//                        List<FlowUserTaskEntity>  flowUserTaskList=userTaskDao.queryListByFlowNodeSNAndObjectSN(condition.getLeftNodeSn(),flowData.getDataSn());
//                        for (var userTask:flowUserTaskList) {
//                            userTask.setTaskState(FlowTaskState.APPROVED.code);
//                            userTask.setRemark(dataHandle.getRemark());
//                            userTaskDao.update(userTask);
//                        }
                        FlowUserTaskEntity flowUserTask = userTaskDao.findBySN(dataHandle.getFlowTaskSn());
                        flowUserTask.setTaskState(FlowTaskState.APPROVED.code);
                        flowUserTask.setRemark(dataHandle.getRemark());
                        flowUserTask.setResult(dataHandle.getPassResult());
                        flowUserTask.setProcessTime(new Date());
                        userTaskDao.update(flowUserTask);
                        pojoSvcDao.updateBySn(flowData.getDataType(), (short) 2, flowData.getDataSn(), stateField, snField);
                        //结束此数据的所有代办，结束流程
                        userTaskDao.updateStateByObjectSN(flowData.getDataSn());

                        //创建消息提醒
                        sendMsg(MsgType.Flow, flowUserTask.getUserSN(), "审批提醒", user.getUserName() + "同意了" + flowUserTask.getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark() + "。流程结束！");

                    }
                } else {
                    //等待其他人进行审核
                    FlowUserTaskEntity userTaskEntity = userTaskDao.findBySN(dataHandle.getFlowTaskSn());
                    userTaskEntity.setTaskState(FlowTaskState.APPROVED.code);
                    userTaskEntity.setRemark(dataHandle.getRemark());
                    userTaskEntity.setResult(dataHandle.getPassResult());
                    userTaskEntity.setProcessTime(new Date());
                    int result = userTaskDao.update(userTaskEntity);
                    //创建消息提醒
                    sendMsg(MsgType.Flow, userTaskEntity.getUserSN(), "审批提醒", user.getUserName() + "同意了" + userTaskEntity.getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark());
                }
            }

        } else if (dataHandle.getPassResult() == FlowDataHandleType.No.code) {
            // 拒绝
            if (nowNode.getPassType() == FlowNodePassType.Or.getCode()) {
                //审批拒绝产生日志 记录拒绝信息 ，判断是否全部人员都拒绝
                //判断是否全部人员都拒绝
                List<FlowLogEntity> noFlowLog = flowLogList.stream().filter(t -> t.getResult() == FlowDataHandleType.No.code).toList();
                boolean passResult = matchFlowHandlerUserSn(handlers, noFlowLog);
                FlowUserTaskEntity userTaskEntity = userTaskDao.findBySN(dataHandle.getFlowTaskSn());
                if (passResult) {
                    flowData.setState(FlowDataState.Rejected.code);

                    pojoSvcDao.updateBySn(flowData.getDataType(), (short) 0, flowData.getDataSn(), stateField, snField);
                    //结束此数据的所有代办，流程结束
                    userTaskDao.updateStateByObjectSN(flowData.getDataSn());
                    //创建消息提醒
                    sendMsg(MsgType.Flow, userTaskEntity.getUserSN(), "审批提醒", user.getUserName() + "拒绝了" + userTaskEntity.getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark());
                } else {
                    flowData.setState(FlowDataState.Rejected.code);
                    userTaskEntity.setTaskState(FlowTaskState.REJECTED.code);
                    userTaskEntity.setRemark(dataHandle.getRemark());
                    userTaskEntity.setResult(dataHandle.getPassResult());
                    userTaskEntity.setProcessTime(new Date());
                    int result = userTaskDao.update(userTaskEntity);
                    pojoSvcDao.updateBySn(flowData.getDataType(), (short) 0, flowData.getDataSn(), stateField, snField);
                    //结束此数据的所有代办，流程结束
                    userTaskDao.updateStateByObjectSN(flowData.getDataSn());
                    //创建消息提醒
                    sendMsg(MsgType.Flow, userTaskEntity.getUserSN(), "审批提醒", user.getUserName() + "拒绝了" + userTaskEntity.getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark());
                }
            } else if (nowNode.getPassType() == FlowNodePassType.And.getCode()) {
                flowData.setState(FlowDataState.Rejected.code);
                FlowUserTaskEntity userTaskEntity = userTaskDao.findBySN(dataHandle.getFlowTaskSn());
                userTaskEntity.setTaskState(FlowTaskState.REJECTED.code);
                userTaskEntity.setRemark(dataHandle.getRemark());
                userTaskEntity.setResult(dataHandle.getPassResult());
                userTaskEntity.setProcessTime(new Date());
                int result = userTaskDao.update(userTaskEntity);
                pojoSvcDao.updateBySn(flowData.getDataType(), (short) 0, flowData.getDataSn(), stateField, snField);
                //结束此数据的所有代办，流程结束1
                userTaskDao.updateStateByObjectSN(flowData.getDataSn());

                //创建消息提醒
                sendMsg(MsgType.Flow, userTaskEntity.getUserSN(), "审批提醒", user.getUserName() + "拒绝了" + userTaskEntity.getDataType() + "的审批,数据编号为" + flowData.getDataSn() + ";审批意见为:" + dataHandle.getRemark());
            }
        }
        return dao.update(flowData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    public Integer startFlow(FlowInfoEntity flowInfo, Object data) throws Exception {
        Integer result = 0;
        String flowInfoSn = flowInfo.getSn();
        String nodeSn = "";
        String dataSn = "";
        Map<String, Object> map = Tl.toMap(data);
        if (map.get("state").toString().equals("1") || map.get("state").toString().equals("2")) {
            dataSn = map.get("sn").toString();
            //结束此数据的所有代办，并重新开启流程
            userTaskDao.updateStateByObjectSN(dataSn);
            FlowNodeEntity timerNode = nodeService.findTimerNodeByFlowInfoSn(flowInfoSn);
            if (timerNode != null) {
                nodeSn = nodeService.findNextNode(timerNode).getSn();
            }
            FlowDataEntity flowData = new FlowDataEntity();
            flowData.setSn(queryNewSN(FlowDataEntity.class));
            flowData.setDataSn(dataSn);
            flowData.setFlowSn(flowInfoSn);
            //todo: 06/29 数据连接
            flowData.setDataLink("");
            flowData.setDataType(flowInfo.getDataTypeCode());
            flowData.setNodeSn(nodeSn);
            flowData.setState(FlowDataState.Circulation.code);
            flowData.setPYFieldValue();
            result = dao.insertNew(flowData);
            if (result > 0) {
                List<FlowNodeHandlersEntity> nodeHandlersEntities = handlersService.findByNodeSn(nodeSn);

                if (nodeHandlersEntities != null && nodeHandlersEntities.size() > 0) {
                    LoginUser loginUser = HsApp.getCurrent();
                    for (FlowNodeHandlersEntity nodeHandlerEntity : nodeHandlersEntities) {

                        FlowUserTaskEntity userTaskEntity = new FlowUserTaskEntity();
                        userTaskEntity.setDataType(flowInfo.getDataTypeName());
                        userTaskEntity.setDataCode(flowInfo.getDataTypeCode());
                        userTaskEntity.setHandlerUserSN(nodeHandlerEntity.getParticipantSN());
                        userTaskEntity.setCreateTime(new Date());
                        userTaskEntity.setFlowNodeSN(nodeSn);
                        userTaskEntity.setFlowDataSN(flowData.getSn());
                        userTaskEntity.setFlowSN(flowInfoSn);
                        if (loginUser != null) {
                            userTaskEntity.setUserSN(loginUser.getUserSN());
                            userTaskEntity.setUserName(loginUser.getUserName());

                        }
                        userTaskEntity.setObjectSN(flowData.getDataSn());
                        userTaskEntity.setTaskState(FlowTaskState.UNREAD.code);
                        userTaskEntity.setSn(queryNewSN(FlowUserTaskEntity.class));
                        //待办事项创建
                        userTaskDao.insertNew(userTaskEntity);
                        sendMsg(MsgType.Flow, nodeHandlerEntity.getParticipantSN(), "审批提醒", loginUser.getUserName() + "向你发起了" + flowInfo.getDataTypeName() + "的审批,数据编号为" + flowData.getDataSn());
                    }
                    FlowLogEntity logEntity = new FlowLogEntity();
                    if (loginUser != null) {
                        logEntity.setUserSN(loginUser.getUserSN());
                        logEntity.setUserName(loginUser.getUserName());
                    }
                    logEntity.setSn(queryNewSN(FlowLogEntity.class));
                    logEntity.setFlowDataSn(flowData.getSn());
                    logEntity.setFlowSN(flowInfoSn);
                    logEntity.setPYFieldValue();
                    logEntity.setHandlerTime(new Date());
                    logEntity.setResult(FlowDataHandleType.Ok.code);
                    logEntity.setNodeSN(nodeSn);
                    logEntity.setActionType((short) 0);
                    //流程日志创建
                    flowLogService.saveFlowLog(logEntity);

                    var dete = pojoSvcDao.updateBySn(flowData.getDataType(), (short) 1, flowData.getDataSn(), stateField, snField);
                } else {
                    pojoSvcDao.updateBySn(flowData.getDataType(), (short) 2, flowData.getDataSn(), stateField, snField);
                }
                //dao.changeStateByDataSnAndState(dataSn, FlowDataState.Circulation.code,FlowDataState.PassiveOver.code);

                //todo:06/29 被动中止 增加日志输出
            }
            return result;
        } else {
            return 0;
        }

    }

    private Integer startStandFlow(FlowInfoEntity flowInfo, Object data) throws Exception {
        Integer result = 0;
        String flowInfoSn = flowInfo.getSn();
        String nodeSn = "";
        String dataSn = "";
        Map<String, Object> map = Tl.toMap(data);
        if (map.get("state").toString().equals("1") || map.get("state").toString().equals("2")) {
            dataSn = map.get("sn").toString();
            //结束此数据的所有代办，并重新开启流程
            userTaskDao.updateStateByObjectSN(dataSn);
            FlowNodeEntity timerNode = nodeService.findTimerNodeByFlowInfoSn(flowInfoSn);
            if (timerNode != null) {
                nodeSn = nodeService.findNextNode(timerNode).getSn();
            }
            FlowDataEntity flowData = new FlowDataEntity();
            flowData.setSn(queryNewSN(FlowDataEntity.class));
            flowData.setDataSn(dataSn);
            flowData.setFlowSn(flowInfoSn);
            //todo: 06/29 数据连接
            flowData.setDataLink("");
            flowData.setDataType(flowInfo.getDataTypeCode());
            flowData.setNodeSn(nodeSn);
            flowData.setState(FlowDataState.Circulation.code);
            flowData.setPYFieldValue();
            result = dao.insertNew(flowData);
            if (result > 0) {
                List<FlowNodeHandlersEntity> nodeHandlersEntities = handlersService.findByNodeSn(nodeSn);

                if (nodeHandlersEntities != null && nodeHandlersEntities.size() > 0) {
                    LoginUser loginUser = HsApp.getCurrent();
                    for (FlowNodeHandlersEntity nodeHandlerEntity : nodeHandlersEntities) {

                        FlowUserTaskEntity userTaskEntity = new FlowUserTaskEntity();
                        userTaskEntity.setDataType(flowInfo.getDataTypeName());
                        userTaskEntity.setDataCode(flowInfo.getDataTypeCode());
                        userTaskEntity.setHandlerUserSN(nodeHandlerEntity.getParticipantSN());
                        userTaskEntity.setCreateTime(new Date());
                        userTaskEntity.setFlowNodeSN(nodeSn);
                        userTaskEntity.setFlowDataSN(flowData.getSn());
                        userTaskEntity.setFlowSN(flowInfoSn);
                        if (loginUser != null) {
                            userTaskEntity.setUserSN(loginUser.getUserSN());
                            userTaskEntity.setUserName(loginUser.getUserName());

                        }
                        userTaskEntity.setObjectSN(flowData.getDataSn());
                        userTaskEntity.setTaskState(FlowTaskState.UNREAD.code);
                        userTaskEntity.setSn(queryNewSN(FlowUserTaskEntity.class));
                        //待办事项创建
                        userTaskDao.insertNew(userTaskEntity);
                        sendMsg(MsgType.Flow, nodeHandlerEntity.getParticipantSN(), "审批提醒", loginUser.getUserName() + "向你发起了" + flowInfo.getDataTypeName() + "的审批,数据编号为" + flowData.getDataSn());
                    }
                    FlowLogEntity logEntity = new FlowLogEntity();
                    if (loginUser != null) {
                        logEntity.setUserSN(loginUser.getUserSN());
                        logEntity.setUserName(loginUser.getUserName());
                    }
                    logEntity.setSn(queryNewSN(FlowLogEntity.class));
                    logEntity.setFlowDataSn(flowData.getSn());
                    logEntity.setFlowSN(flowInfoSn);
                    logEntity.setPYFieldValue();
                    logEntity.setHandlerTime(new Date());
                    logEntity.setResult(FlowDataHandleType.Ok.code);
                    logEntity.setNodeSN(nodeSn);
                    logEntity.setActionType((short) 0);
                    //流程日志创建
                    flowLogService.saveFlowLog(logEntity);

                    pojoSvcDao.updateBySn(flowData.getDataType(), (short) 1, flowData.getDataSn(), stateField, snField);
                } else {
                    pojoSvcDao.updateBySn(flowData.getDataType(), (short) 2, flowData.getDataSn(), stateField, snField);
                }
                //dao.changeStateByDataSnAndState(dataSn, FlowDataState.Circulation.code,FlowDataState.PassiveOver.code);

                //todo:06/29 被动中止 增加日志输出
            }
            return result;
        } else {
            return 0;
        }
    }

    @Override
    public Integer updateStateByState(short setState, short whereState) {
        return dao.updateStateByState(setState, whereState);
    }


    private boolean matchFlowHandlerUserSn(List<FlowNodeHandlersEntity> handlers, List<FlowLogEntity> okFlowLog) {
        boolean passResult = false;
        for (FlowNodeHandlersEntity handler : handlers) {
            List<FlowLogEntity> handlerInFlowLog = okFlowLog.stream().filter(t -> t.getUserSN().equals(handler.getParticipantSN())).toList();
            if (handlerInFlowLog.size() > 0) {
                passResult = true;
            } else {
                passResult = false;
            }
        }

        return passResult;
    }

    public int closeDateTask(String table, short state, String sn) {
        return pojoSvcDao.updateBySn(table, state, sn, stateField, snField);
    }

    private void flowPassed(FlowDataEntity flowData, FlowConditionEntity condition) {
        if (condition == null) {
            // 当前节点为最后一个节点
            flowData.setState(FlowDataState.Over.code);
        } else if (Tl.isEmpty(condition.getRightNodeSn())) {
            flowData.setState(FlowDataState.Over.code);
        } else {
            //有下一个节点
            FlowNodeEntity nextNode = nodeService.findBySn(condition.getRightNodeSn());
            if (nextNode.getNodeName().contains("结束")) {
                flowData.setState(FlowDataState.Over.code);
            } else {
                flowData.setNodeSn(nextNode.getSn());
            }

        }
    }


    public void updataKc(FlowDataEntity entity) {
        try {
            var recordEntity = finishProductWarehouseRecordDao.findBySN(entity.getDataSn());
            if (recordEntity != null && !recordEntity.getRemark().equals("对装出货")) {
                var productInventory = inventoryDao.findByWareHouseSNAndProductSN(recordEntity.getWareHouseSN(), recordEntity.getProductSN(), recordEntity.getPackageName(), recordEntity.getPackageSN());
                if (productInventory != null) {
                    var query = productInventory.getWeight();
                    //修改成品库存数量
                    if (recordEntity.getType() == 0) {
                        productInventory.setWeight(productInventory.getWeight().add(recordEntity.getWeight()));
                        productInventory.setQuantity(productInventory.getQuantity().add(recordEntity.getQuantity()));
                    } else {
                        productInventory.setWeight(productInventory.getWeight().subtract(recordEntity.getWeight()));
                        productInventory.setQuantity(productInventory.getQuantity().subtract(recordEntity.getQuantity()));
                    }
                    inventoryDao.update(productInventory);

                    if (recordEntity.getType() == 0) {
                        writeLog(ActionType.UPDATE, productInventory.getSn(), "创建入库记录增加库存，库存由" + query.setScale(2, RoundingMode.DOWN)
                                        + "变为" + productInventory.getWeight().setScale(2, RoundingMode.DOWN) + ",入库记录编号为" + recordEntity.getSn()
                                        + "&&" + recordEntity.getTruckPlate() + "&&" + productInventory.getWeight().setScale(2, RoundingMode.DOWN).subtract(query.setScale(2, RoundingMode.DOWN)),
                                null, FinishProductInventoryEntity.class);
                    } else if (recordEntity.getType() == 1) {
                        writeLog(ActionType.UPDATE, productInventory.getSn(), "创建损耗记录减少库存，库存由" + query.setScale(2, RoundingMode.DOWN)
                                        + "变为" + productInventory.getWeight().setScale(2, RoundingMode.DOWN) + ",出库记录编号为" + recordEntity.getSn()
                                        + "&&" + recordEntity.getTruckPlate() + "&&" + query.setScale(2, RoundingMode.DOWN).subtract(productInventory.getWeight().setScale(2, RoundingMode.DOWN)),
                                null, FinishProductInventoryEntity.class);
                    } else {
                        writeLog(ActionType.UPDATE, productInventory.getSn(), "创建出库记录减少库存，库存由" + query.setScale(2, RoundingMode.DOWN)
                                        + "变为" + productInventory.getWeight().setScale(2, RoundingMode.DOWN) + ",损耗记录编号为" + recordEntity.getSn()
                                        + "&&" + recordEntity.getTruckPlate() + "&&" + query.setScale(2, RoundingMode.DOWN).subtract(productInventory.getWeight().setScale(2, RoundingMode.DOWN)),
                                null, FinishProductInventoryEntity.class);
                    }

                } else {
                    //新建成品库存信息
                    if (recordEntity.getType() == 0) {
                        FinishProductInventoryEntity inventoryEntity = new FinishProductInventoryEntity();
                        inventoryEntity.setSn(queryNewSN(FinishProductInventoryEntity.class));
                        inventoryEntity.setProductName(recordEntity.getProductName());
                        inventoryEntity.setProductSN(recordEntity.getProductSN());
                        inventoryEntity.setProductSpec(recordEntity.getProductSpec());
                        inventoryEntity.setWareHouseName(recordEntity.getWareHouseName());
                        inventoryEntity.setWareHouseSN(recordEntity.getWareHouseSN());
                        inventoryEntity.setQuantity(recordEntity.getQuantity());
                        inventoryEntity.setWeight(recordEntity.getWeight());
                        inventoryEntity.setState((short) 2);
                        inventoryEntity.setPackageSN(recordEntity.getPackageSN());
                        inventoryEntity.setPackageName(recordEntity.getPackageName());
                        inventoryDao.insertNew(inventoryEntity);
                        writeLog(ActionType.CREATE, inventoryEntity.getSn(), "创建入库记录增加库存，库存由0变为" + recordEntity.getWeight() + ",入库记录编号为" + recordEntity.getSn(), null, FinishProductInventoryEntity.class);
                    }
                }
                var warehouse = warehouseDao.findBySN(recordEntity.getWareHouseSN());
                if (warehouse != null) {
                    var query = warehouse.getInventory();
                    var inventory = warehouse.getInventory();
                    if (recordEntity.getType() == 0) {
                        inventory = inventory.add(recordEntity.getWeight());
                        writeLog(ActionType.CREATE, warehouse.getSn(), "同意审批创建入库记录增加库存,库存由" + query.setScale(2) + "变为" + inventory.setScale(2) + ",入库记录编号为" + recordEntity.getSn(), null, WarehouseEntity.class);
                    } else if (recordEntity.getType() == 1) {
                        inventory = inventory.subtract(recordEntity.getWeight());
                        writeLog(ActionType.CREATE, warehouse.getSn(), "同意审批创建损耗记录减少库存,库存由" + query.setScale(2) + "变为" + inventory.setScale(2) + ",入库记录编号为" + recordEntity.getSn(), null, WarehouseEntity.class);
                    } else {
                        inventory = inventory.subtract(recordEntity.getWeight());
                        writeLog(ActionType.CREATE, warehouse.getSn(), "同意审批创建出库记录减少库存,库存由" + query.setScale(2) + "变为" + inventory.setScale(2) + ",入库记录编号为" + recordEntity.getSn(), null, WarehouseEntity.class);
                    }
                    warehouse.setInventory(inventory);
                    warehouseDao.update(warehouse);
                }
            }
        }catch (Exception e){

        }
    }

    @Autowired
    IFinishProductInventoryDao inventoryDao;
    @Autowired
    IWarehouseDao warehouseDao;
    @Autowired
    IFinishProductWarehouseRecordDao finishProductWarehouseRecordDao;

    @Autowired
    public void setProductionTaskDao(IProductionTaskDao productionTaskDao) {
        this.productionTaskDao = productionTaskDao;
    }

    @Autowired
    public void setSaleContractDao(ISaleContractDao saleContractDao) {
        this.saleContractDao = saleContractDao;
    }

    private IProductionTaskDao productionTaskDao;
    private ISaleContractDao saleContractDao;
}